
package com.femass.converter;

import com.femass.model.Evento;
import com.femass.repository.EventoRepository;
import com.femass.util.Repositorios;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(forClass = Evento.class)
public class EventoConverter implements Converter{

    private Repositorios repositorios = new Repositorios();
    private EventoRepository eventoRepo = this.repositorios.getEventos();
       
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if(!value.equals("")){
            return this.eventoRepo.porCodigo(new Integer(value));
        }
        
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if(value!=null){
            Integer codigo = ((Evento)value).getCod();
            return codigo == null ? "" : codigo.toString();
        }
        return null;
    }
    
}
